/*
Run Rollup in watch mode for development.

To specific the package to watch, simply pass its name and the desired build
formats to watch (defaults to "global"):

```
# name supports fuzzy match. will watch all packages with name containing "dom"
yarn dev dom

# specify the format to output
yarn dev core --formats cjs

# Can also drop all __DEV__ blocks with:
__DEV__=false yarn dev
```
*/

// execa 开启子进程 方便进行命令
const execa = require('execa')
const { fuzzyMatchTarget } = require('./utils')
/**
 *  minimist————解析引擎：用于处理命令行调用node指令时，处理node后的一系列参数的模块
 * process.argv.slice(2)：对应执行命令参数位置，从第三个参数起算
 */
const args = require('minimist')(process.argv.slice(2))
/**
 * 获取命令行参数中输入的模块名，默认值vue
 * fuzzyMatchTarget函数在utils.js中，用于对命令行中传入的模块参数进行解析，是否能够匹配根目录下/packages目录下的模块名
 */
const target = args._.length ? fuzzyMatchTarget(args._)[0] : 'vue'
const formats = args.formats || args.f
const sourceMap = args.sourcemap || args.s
/** 
 * nodejs有个内置child_process模块，提供方法在子进程中运行命令脚本。execa为child_process提供包模块器
 * execa.sync(文件，参数？，选项？)：同步执行一个文件
 * execa.sync()会返回标准流
*/
const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)

execa(
  'rollup',
  [
    '-wc',
    '--environment',
    [
      `COMMIT:${commit}`,
      `TARGET:${target}`,
      `FORMATS:${formats || 'global'}`,
      sourceMap ? `SOURCE_MAP:true` : ``
    ]
      .filter(Boolean)
      .join(',')
  ],
  {
    stdio: 'inherit'
  }
)
